package com.qcs.sleep.log;

import android.os.Environment;
import android.util.Log;

import com.qcs.sleep.helper.IOHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by excing on 13-8-23.
 */
public class Log4J { // 日志输入
    static String TAG = Log4J.class.getSimpleName();

    public static void m(String arg0) {
        Log.i(TAG, null == arg0 ? "null" : arg0);
    }

    public static void t(String arg0) {
        Log.e(TAG, null == arg0 ? "null" : arg0);

        String path = getLogPath();
        path += "print.log";
        append(path, arg0, true);
    }

    public static void w(String fn, String arg1) {
        write(fn, arg1);
    }

    public static void e(String arg0) {
        Log.e(TAG, null == arg0 ? "null" : arg0);

        String path = getLogPath();
        path += valueOf(System.currentTimeMillis()) + ".log";
        write(path, arg0);
    }

    public static void e(Exception e) {
        String content = getStackTrace(e);
        Log.e(TAG, content);

        String path = getLogPath();
        path += valueOf(System.currentTimeMillis()) + ".log";

        write(path, content);
    }

    private static String getLogPath() {
        String path = Environment.getExternalStorageDirectory().getPath() + File.separator
                                + Log4J.class.getPackage().getName() + File.separator;

        IOHelper.isExistForDir(path);
        return path;
    }

    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName 文件名
     * @param content  内容
     */
    private static void append(String fileName, String content, boolean isAppend) {
        FileWriter writer = null;

        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, isAppend);
            writer.write(null == content ? "null" : content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从头开始写文件：使用FileWriter
     *
     * @param fileName 文件名
     * @param content  内容
     */
    private static void write(String fileName, String content) {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(new File(fileName));
            fos.write((null == content ? "null" : content).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String valueOf(long time) { // 将时间转换为字符串格式，格式为yyyyMMddHHmmss
        return valueOf(time, "yyyyMMddHHmmss");
    }

    private static String valueOf(long time, String pattern) { // 将时间转换为字符串格式
        if (0L >= time) return null;

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = new Date(time);
        return format.format(date);
    }

    /**
     * 获取异常的堆栈信息
     *
     * @param t 异常
     * @return 堆栈信息
     */
    private static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try {
            t.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }


}
